package com.phantasy.scene.sceneUnit.baseUnit.things.avatar
{
	import com.phantasy.Style;
	import com.phantasy.globals.GameConfig;
	import com.phantasy.scene.sceneUnit.baseUnit.OnlyIDCreater;
	import com.phantasy.scene.sceneUnit.baseUnit.things.ThingFrame;
	import com.phantasy.scene.sceneUnit.baseUnit.things.common.BitmapClipData;
	import com.phantasy.scene.sceneUnit.baseUnit.things.common.BitmapFrame;
	import com.phantasy.scene.sceneUnit.baseUnit.things.resource.SourceManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	
	/**
	 *化身分解体 
	 * @author Administrator
	 * 
	 */	
	public class Part extends Bitmap
	{
		public static var PLAY_STATE:int = 0;
		public static var STOP_STATE:int = 1;
		
		public var endFunction:Function;
		public var state:int;
		public var onlyKey:String;
		protected var _url:String;
		protected var _action:String;
		protected var _dir:int;
		protected var _actionAndDir:String;
		protected var _speed:int;
		protected var _speedCount:int;
		protected var _isLoop:Boolean;
		protected var _maxFrame:int;
		protected var _currentFrame:int = 0;
		protected var _startFrame:int = 0;
		protected var _endFrame:int = 0;
		protected var _hold:int = 0;

		public function Part()
		{
			this.state = STOP_STATE;
			this._actionAndDir = AvatarConstant.ACTION_DEFULT + "_d" + AvatarConstant.DIR_UP;
			super();
			this.onlyKey = OnlyIDCreater.createID();
		}
		
		/**
		 *清理 
		 * 
		 */		
		public function clean():void
		{
			if(this.bitmapData!=null){
				this.x= this.y = 0;
				this.bitmapData=null;
			}
		}
		
		public function setURL(url:String):void
		{
			this._url = url;	
		}
		
		public function stop():void
		{
			if (this.state == STOP_STATE)
			{
				return;
			}
			this.state = STOP_STATE;
			this.removeFromFrame();
		}
		
		public function play(_arg1:String, _arg2:String, _arg3:int, _arg4:int=4, _arg5:Boolean=false):void
		{
			if (_arg1 == "")
			{
				this.stop();
				this.clean();
				return;
			};
			this._url = _arg1;
			this._currentFrame = 0;
			this.setAction(_arg2);
			this._actionAndDir = _arg2.concat("_d").concat(_arg3);
			this._speed = _arg4;
			this._isLoop = _arg5;
			this._startFrame = 0;
			this._endFrame = this._maxFrame;
			this.addToFrame();
		}
		
		public function setAction(_arg1:String):void
		{
			this._action = _arg1;
			var _local2:BitmapClipData = SourceManager.getInstance().getResource(this._url);
			if (_local2 != null)
			{
				this._maxFrame = (_local2.getLight(this._action) - 1);
			}
		}
		
		protected function addToFrame():void
		{
			this.state = PLAY_STATE;
			this._speedCount = 0;
			this.setCurrentFrame();
			ThingFrame.getInstance().add(this.onlyKey, this.step);
		}
		
		protected function removeFromFrame():void
		{
			ThingFrame.getInstance().remove(this.onlyKey);
		}
		
		public function setCurrentFrame():void
		{
			var _local2:BitmapFrame;
			var _local1:BitmapClipData = SourceManager.getInstance().getResource(this._url);
			if (_local1 != null)
			{
				_local2 = _local1.getFrame(this._actionAndDir.concat(("_" + this._currentFrame)));
				if (_local2 == null)
				{
					this.stop();
					this.clean();
					return;
				};
				bitmapData = _local2.data;
				x = _local2.offsetX;
				y = _local2.offsetY;
				this._hold = _local2.hold;
			}
			else
			{
				this.stop();
				this.clean();
			};
		}
		
		public function step():void
		{
			this._speedCount++;
			if (this._speedCount >= this.checkSpeed())
			{
				this.updataStep();
				this._speedCount = 0;
			}
		}
		
		private function checkSpeed():int
		{
			return ((this._hold + this._speed));
		}
		
		protected function updataStep():void
		{
			if (this._currentFrame >= this._endFrame)
			{
				if (this._isLoop == false)
				{
					this.complete();
					return;
				}
				this._currentFrame = this._startFrame;
			}
			else
			{
				this._currentFrame++;
			}
			this.setCurrentFrame();
		}
		
		protected function complete():void
		{
			this.stop();
			if (((!((this._action == AvatarConstant.ACTION_SIT))) && (!((this._action == AvatarConstant.ACTION_DIE)))))
			{
				this.clean();
			};
			if (this.endFunction != null)
			{
				this.endFunction();
			}
		}
		
		public function setTransparent():void
		{
			if (this.bitmapData == null)
			{
				this.bitmapData = Style.getUIBitmapData(GameConfig.VIEWUI_URL, "Transparent");
				x = -24;
				y = -77;
			}
		}
		
	}
}